package com.jokul.listener;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;

import com.jokul.dao.AccountDaoImpl;
import com.jokul.dao.BlahDaoImpl;
import com.jokul.service.UserService;

/**
 * @author: jokul
 * @2013-3-15 下午4:18:14
 */
@WebListener
public class BlogListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		try {
			Context initialContext = new InitialContext();
			Context envContext = (Context) initialContext
					.lookup("java:/comp/env");
			DataSource dataSource = (DataSource) envContext
					.lookup("jdbc/micro");
			ServletContext servletContext = sce.getServletContext();
			servletContext.setAttribute("userService",
					new UserService(new AccountDaoImpl(dataSource),
							new BlahDaoImpl(dataSource)));
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

}
